#include <iostream>
#include <memory>

#include "../../../common/data_mysql.h"
#include "../../../odb/user.hxx"
#include "log.h"

void user_insert(chat::UserTable& user_table) {
    std::shared_ptr<chat::User> user1 =
        std::make_shared<chat::User>("uid1", "用户1", "123456");
    std::shared_ptr<chat::User> user2 =
        std::make_shared<chat::User>("uid2", "15973989808");

    user_table.insert(user1);
    user_table.insert(user2);
}

void user_update(chat::UserTable& user_table) {
    // std::shared_ptr<chat::User> user1 =
    // user_table.select_by_nickname("用户1"); user1->description("hello
    // world"); user_table.update(user1);

    // std::shared_ptr<chat::User> user2 =
    // user_table.select_by_phone("15973989808"); user2->description("hello
    // linux"); user_table.update(user2);

    std::shared_ptr<chat::User> user3 = user_table.select_by_user_id("uid2");
    user3->description("hello mysql");
    user_table.update(user3);
}

void select_by_ids(chat::UserTable& user_table) {
    const std::vector<std::string> ids = {"uid1", "uid2"};

    std::vector<chat::User> res = user_table.select_multi_by_ids(ids);
    for (auto& user_ptr : res) {
        std::cout << "uid: " << user_ptr.user_id() << std::endl;
        std::cout << "nickname: " << user_ptr.nickname() << std::endl;
        if (user_ptr.phone()) {
            std::cout << "phone: " << *user_ptr.phone() << std::endl;
        }
        if (user_ptr.description()) {
            std::cout << "description: " << *user_ptr.description()
                      << std::endl;
        }

        std::cout << std::endl;
    }
}

int main() {
    chat::initLogger(1, false, " ");

    std::shared_ptr<odb::mysql::database> db = chat::DataBaseFactory::create(
        1, "root", "lwj225315", "chat", "127.0.0.1", 0, "utf8");

    chat::UserTable user_table(db);

    // user_insert(user_table);

    // user_update(user_table);

    select_by_ids(user_table);

    return 0;
}